package com.artifex.mupdf.viewer;

import static com.artifex.mupdf.viewer.SettingsItem.TYPE.DIVIDER;
import static com.artifex.mupdf.viewer.SettingsItem.TYPE.ENCODE;
import static com.artifex.mupdf.viewer.SettingsItem.TYPE.ENGINE;
import static com.artifex.mupdf.viewer.SettingsItem.TYPE.FROM;
import static com.artifex.mupdf.viewer.SettingsItem.TYPE.HEADER;
import static com.artifex.mupdf.viewer.SettingsItem.TYPE.SETTING;
import static com.artifex.mupdf.viewer.SettingsItem.TYPE.TO;
import static com.artifex.mupdf.viewer.SettingsItem.TYPE.TOGGLE;
import static com.artifex.mupdf.viewer.SettingsItem.TYPE.fSPEED;
import static com.artifex.mupdf.viewer.SettingsItem.TYPE.tSPEED;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.widget.SwitchCompat;

import java.util.List;

public class SettingsAdapter extends BaseAdapter {
    private final List<SettingsItem> items;
    private final Context context;
    private final OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(SettingsItem item, int position);
    }

    public SettingsAdapter(Context context, List<SettingsItem> items, OnItemClickListener listener) {
        this.context = context;
        this.items = items;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public SettingsItem getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SettingsItem item = getItem(position);

        if (DIVIDER == item.type) {
            if (convertView == null || !DIVIDER.equals(convertView.getTag())) {
                convertView = LayoutInflater.from(context).inflate(R.layout.item_divider, parent, false);
                convertView.setTag(DIVIDER);
            }
            return convertView;
        }

        if (convertView == null || DIVIDER.equals(convertView.getTag())) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_setting, parent, false);
            convertView.setTag(SETTING);
        }

        TextView titleText = convertView.findViewById(R.id.title_text);
        TextView valueText = convertView.findViewById(R.id.value_text);
        ImageView checkIcon = convertView.findViewById(R.id.check_icon);

        if(fSPEED == item.type || tSPEED == item.type) {
            titleText.setText(String.valueOf(item.speed));
        } else if(FROM == item.type || TO == item.type) {
            titleText.setText(item.language.getLanguage());
        } else if(ENGINE == item.type || HEADER == item.type) {
            titleText.setText(item.name);
        } else if(ENCODE == item.type) {
            titleText.setText(item.encode);
        }

        // 根据类型显示不同的UI
        if(HEADER.equals(item.type)) {
            valueText.setVisibility(View.GONE);
            checkIcon.setVisibility(View.GONE);
        } else {
            valueText.setVisibility(View.VISIBLE);
            checkIcon.setVisibility(View.VISIBLE);

            valueText.setText(item.packageName);
            checkIcon.setVisibility(item.selected ? View.VISIBLE : View.INVISIBLE);
        }
        convertView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(item, position);
            }
        });
        convertView.setVisibility(item.visibility);

        return convertView;
    }

    @Override
    public int getViewTypeCount() {
        return 2; // 分隔线和设置项
    }

    @Override
    public int getItemViewType(int position) {
        return DIVIDER.equals(getItem(position).type) ? 0 : 1;
    }
}